package com.lyy.weather.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;

import com.lyy.weather.TIme.TimeUtil;
import com.lyy.weather.activity.MainActivity;
import com.lyy.weather.bean.AirIndices;
import com.lyy.weather.bean.City;
import com.lyy.weather.bean.LifeIndices;
import com.lyy.weather.bean.WeatherDaily;
import com.lyy.weather.bean.WeatherHourly;
import com.lyy.weather.bean.WeatherTimely;
import com.lyy.weather.network.HttpUtil;

import java.util.ArrayList;
import java.util.List;

//对数据库增删查改的工具类包
public class DatabaseUtil {

    private Context mContext;
    private DatabaHelper dbHelper;

    public Context getmContext() {
        return mContext;
    }

    public DatabaHelper getDbHelper() {
        return dbHelper;
    }

    public DatabaseUtil(Context mContext, DatabaHelper dbHelper) {
        this.mContext = mContext;
        this.dbHelper=dbHelper;
        //this.locationId=locationId;
    }

    public DatabaseUtil(DatabaHelper dbHelper) {
        this.dbHelper = dbHelper;
    }

    //根据Request的不同，选择不同的添加数据的方法~
    //object其实是携带数据的java bean，将在嵌套的具体方法中向下转型还原
    public void addSpecificTable(int requestType, Object object,String locationId) {

        //根据请求类型做出具体方法，传入bean类，会在下面向下转型
        switch (requestType) {
            case HttpUtil.MyRequestType.CITY_REQUEST:
                addCity(object);
                break;
            case HttpUtil.MyRequestType.AIR_INDICES_REQUEST:
                addAirIndices(object,locationId);
                break;
            case HttpUtil.MyRequestType.LIFE_INDICES_REQUEST:
                addLifeIndices(object,locationId);
                break;
            case HttpUtil.MyRequestType.WEATHER_TIMELY_REQUEST:
                addWeatherTimely(object,locationId);
                break;
            case HttpUtil.MyRequestType.WEATHER_HOURLY_REQUEST:
                addWeatherHourly(object,locationId);
                break;
            case HttpUtil.MyRequestType.WEATEHR_WEEKLY_REQUEST:
                addWeatherDaily(object,locationId);
                break;
            case HttpUtil.MyRequestType.ALL_DATA_REQUEST:
                    addTime();
            default:


        }
    }

//下面是把数据放入数据库的各类方法
    public void addTime(){
        Log.d("TAG", "start Time adding~ ");
//        TimeUtil timeUtil=new TimeUtil();
        String updateTime=TimeUtil.getDateString(TimeUtil.getDate());
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        ContentValues values=new ContentValues();
       // Log.d("TAG", updateTime);
        values.put("time",updateTime);
        db.insert("TimeRecord",null,values);
        values.clear();
        db.close();

    }

    public  void addCity(Object object){
        //Log.d("TAG", "addCity OKK~");

        SQLiteDatabase db=dbHelper.getReadableDatabase();
        ContentValues values=new ContentValues();
        //向下转型了
        City city=(City) object;
        //组装数据
        values.put("name",city.getName());
        values.put("id",city.getId());
        values.put("adm2",city.getAdm2());
        values.put("adm1",city.getAdm1());
        values.put("country",city.getCountry());
        db.insert("City",null,values);
        values.clear();//db.close();

    }

    public void addLifeIndices(Object object,String locationId){
       // Log.d("TAG", "addLifeIndices OKK~");

        SQLiteDatabase db=dbHelper.getReadableDatabase();
        ContentValues values=new ContentValues();

        LifeIndices.DifferentIndices indices=(LifeIndices.DifferentIndices)object;//向下转型
       // db.delete("LifeIndices","locationId=?",new String[]{locationId});
        //组装数据
        values.put("locationId",locationId);
        values.put("date",indices.getDate());
        values.put("type",indices.getType());
        values.put("name",indices.getName());
        values.put("level",indices.getLevel());
        values.put("category",indices.getCategory());
        values.put("text",indices.getText());
        //打包添加数据
        db.insert("LifeIndices",null,values);
        values.clear();db.close();
    }

    public void addAirIndices(Object object,String locationId){
       // Log.d("TAG", "addAirIndices OKK~");
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        ContentValues values=new ContentValues();
       // db.delete("AirIndices","locationId=?",new String[]{locationId});
        AirIndices indices=(AirIndices)object;//向下转型还原
        //组装数据
        values.put("locationId",locationId);
        values.put("pubtime",indices.getPubTime());
        values.put("aqi",indices.getAqi());
        values.put("level",indices.getLevel());
        values.put("category",indices.getCategory());
        values.put("mprimary",indices.getPrimary());
        values.put("pm10",indices.getPm10());
        values.put("pm2p5",indices.getPm2p5());
        values.put("no2",indices.getNo2());
        values.put("so2",indices.getSo2());
        values.put("co",indices.getCo());
        values.put("o3",indices.getO3());

        db.insert("AirIndices",null,values);
        values.clear();db.close();
    }

    public void addWeatherDaily(Object object,String locationId){
       // Log.d("TAG", "addWeatherDaily OKK~");
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        ContentValues values=new ContentValues();
        WeatherDaily weatherDaily=(WeatherDaily)object;//向下转型还原
        //组装数据
        values.put("locationId",locationId);
        values.put("fxDate",weatherDaily.getFxDate());
        values.put("sunrise",weatherDaily.getSunrise());
        values.put("sunset",weatherDaily.getSunset());
        values.put("moonrise",weatherDaily.getMoonrise());
        values.put("moonset",weatherDaily.getMoonset());
        values.put("moonPhase",weatherDaily.getMoonPhase());
        values.put("moonPhaseIcon",weatherDaily.getMoonPhaseIcon());
        values.put("tempMax",weatherDaily.getTempMax());
        values.put("tempMin",weatherDaily.getTempMin());
        values.put("iconDay",weatherDaily.getIconDay());
        values.put("textDay",weatherDaily.getTextDay());
        values.put("iconNight",weatherDaily.getIconNight());
        values.put("textNight",weatherDaily.getTextNight());
        values.put("wind360Day",weatherDaily.getWind360Day());
        values.put("windDirDay",weatherDaily.getWindDirDay());
        values.put("windScaleDay",weatherDaily.getWindScaleDay());
        values.put("windSpeedDay",weatherDaily.getWindSpeedDay());
        values.put("humidity",weatherDaily.getHumidity());
        values.put("precip",weatherDaily.getPrecip());
        values.put("pressure",weatherDaily.getPressure());
        values.put("vis",weatherDaily.getVis());
        values.put("cloud",weatherDaily.getCloud());
        values.put("uvIndex",weatherDaily.getUvIndex());

        db.insert("WeatherDaily",null,values);
        values.clear();db.close();
    }

    public void addWeatherHourly(Object object,String locationId){
       // Log.d("TAG", "addWeatherHourly OKK~");
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        ContentValues values=new ContentValues();
        WeatherHourly weatherHourly=(WeatherHourly)object;//向下转型还原
        //组装数据
        values.put("locationId",locationId);
        values.put("fxTime",weatherHourly.getFxTime());
        values.put("temperature",weatherHourly.getTemp());
        values.put("icon",weatherHourly.getIcon());
        values.put("text",weatherHourly.getText());
        values.put("wind360",weatherHourly.getWind360());
        values.put("windDir",weatherHourly.getWindDir());
        values.put("windScale",weatherHourly.getWindScale());
        values.put("windSpeed",weatherHourly.getWindSpeed());
        values.put("humidity",weatherHourly.getHumidity());
        values.put("precip",weatherHourly.getPrecip());
        values.put("pressure",weatherHourly.getPressure());
        values.put("pop",weatherHourly.getPop());
        values.put("cloud",weatherHourly.getCloud());
        values.put("dew",weatherHourly.getDew());

        db.insert("WeatherHourly",null,values);
        values.clear();db.close();

    }

    public  void addWeatherTimely(Object object,String locationId){
        //Log.d("TAG", "addWeatherTimely OKK~");
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        ContentValues values=new ContentValues();
        WeatherTimely weatherTimely=(WeatherTimely)object;//向下转型还原
        //组装数据
        values.put("locationId",locationId);
        values.put("obsTime",weatherTimely.getObsTime());
        values.put("temperature",weatherTimely.getTemp());
        values.put("feelsLike",weatherTimely.getFeelsLike());
        values.put("icon",weatherTimely.getIcon());
        values.put("text",weatherTimely.getText());
        values.put("wind360",weatherTimely.getWind360());
        values.put("windDir",weatherTimely.getWindDir());
        values.put("windScale",weatherTimely.getWindScale());
        values.put("windSpeed",weatherTimely.getWindSpeed());
        values.put("humidity",weatherTimely.getHumidity());
        values.put("precip",weatherTimely.getPrecip());
        values.put("pressure",weatherTimely.getPressure());
        values.put("vis",weatherTimely.getVis());
        values.put("cloud",weatherTimely.getCloud());
        values.put("dew",weatherTimely.getDew());

        db.insert("WeatherTimely",null,values);
        values.clear();db.close();
    }

    //下面是对数据库中各表进行查询的方法
//查询时间表中上一次的更新时间
    public String searchLatestTime(){
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        Cursor cursor=db.query("TimeRecord",null,null,null,null,
                null,null);
        if(cursor.moveToLast())
        {
            int index=cursor.getColumnIndex("time");
            return cursor.getString(index);
        }db.close();

        return null;//如果返回空，则此表为空表，直接更新
    }

    //查询24h天气最近的预报时间
    public String searchLatest24hTime(String locationId){
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        String time;
        List<WeatherHourly> weatherHourlist=searchWeatherHourly(locationId);
        time=weatherHourlist.get(0).getFxTime();db.close();
        return time;
    }
//查询指定城市信息，返回装载有信息的City对象
    public Object searchCity(String locationId){
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        Cursor cursor=db.query("City",null,"id=?",new String[] {locationId},null,null,
                null);
        //用于返回的对象，下面为其装载数据
        City city=new City();
        if(cursor.moveToFirst()){
            int index=cursor.getColumnIndex("mid");
            city.setMid(cursor.getInt(index));
            index=cursor.getColumnIndex("name");
            city.setName(cursor.getString(index));
            index=cursor.getColumnIndex("adm1");
            city.setAdm1(cursor.getString(index));
            index=cursor.getColumnIndex("adm2");
            city.setAdm2(cursor.getString(index));
            index=cursor.getColumnIndex("country");
            city.setCountry(cursor.getString(index));
            index=cursor.getColumnIndex("id");
            city.setId(cursor.getString(index));
            cursor.close();
            db.close();
            return city;
        }else {db.close();
            cursor.close();
            return null;//返回空，即数据库中没有这段数据
        }


    }

//获取所有的城市编号
    public  List<String> getAlllocationId(){
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        Cursor cursor=db.query("City",null,null,null,null,null,
                null);
        List<String> list=new ArrayList<>();
        int index;
        if(cursor.moveToFirst())
        {
            do{
                index=cursor.getColumnIndex("id");
                list.add(cursor.getString(index));
            }while(cursor.moveToNext());db.close();
            return list;
        }else{db.close();
            return null;//若为空，意味着数据库内一个城市信息都没有
        }
    }

//查询指定城市的天气生活指数，返回装载有信息的LifeIndices对象
    public Object searchLifeIndices(String locationId){


        SQLiteDatabase db=dbHelper.getReadableDatabase();
        Cursor cursor=db.query("LifeIndices",null,"locationId=?",new String[]{locationId},
                null,null,null);

        LifeIndices indices=new LifeIndices();//要返回的对象
        List<LifeIndices.DifferentIndices> list=new ArrayList<>();
        int index;
        if(cursor.moveToFirst()){
            do{
                LifeIndices.DifferentIndices differentIndices=new LifeIndices.DifferentIndices();//装载数据
                index=cursor.getColumnIndex("mid");
                differentIndices.setMid(cursor.getInt(index));

                index=cursor.getColumnIndex("type");
                differentIndices.setType(cursor.getInt(index));

                index=cursor.getColumnIndex("name");
                differentIndices.setName(cursor.getString(index));

                index=cursor.getColumnIndex("level");
                differentIndices.setLevel(cursor.getString(index));

                index=cursor.getColumnIndex("category");
                differentIndices.setCategory(cursor.getString(index));

                index=cursor.getColumnIndex("text");
                differentIndices.setText(cursor.getString(index));

                list.add(differentIndices);

            }while(cursor.moveToNext());
        }else{cursor.close();db.close();
            return null;//返回空意味着找不到数据
        }
        cursor.close();db.close();
        indices.setIndicesList(list);
        indices.setLocationId(locationId);
        return indices;

    }

//查询指定城市的空气指数，返回装载有信息AirIndices对象
    public Object searchAirIndices(String locationId){
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        Cursor cursor=db.query("AirIndices",null,"locationID=?",
                new String[]{locationId},null,null,null);

        AirIndices indices=new AirIndices();
        int index;
        if(cursor.moveToFirst()){
            index=cursor.getColumnIndex("mid");
            indices.setMid(cursor.getInt(index));

            indices.setLocationId(locationId);

            index=cursor.getColumnIndex("pubtime");
            indices.setPubTime(cursor.getString(index));
            index=cursor.getColumnIndex("aqi");
            indices.setAqi(cursor.getString(index));
            index=cursor.getColumnIndex("level");
            indices.setLevel(cursor.getString(index));
            index=cursor.getColumnIndex("category");
            indices.setCategory(cursor.getString(index));
            index=cursor.getColumnIndex("mprimary");
            indices.setPrimary(cursor.getString(index));
            index=cursor.getColumnIndex("pm10");
            indices.setPm10(cursor.getString(index));
            index=cursor.getColumnIndex("pm2p5");
            indices.setPm2p5(cursor.getString(index));
            index=cursor.getColumnIndex("no2");
            indices.setNo2(cursor.getString(index));
            index=cursor.getColumnIndex("so2");
            indices.setSo2(cursor.getString(index));
            index=cursor.getColumnIndex("co");
            indices.setCo(cursor.getString(index));
            index=cursor.getColumnIndex("o3");
            indices.setO3(cursor.getString(index));
            cursor.close();db.close();
            return indices;
        }else{cursor.close();db.close();
            return null;//若为空，表示表中无数据
        }
    }

//查询指定城市的实时天气，返回装载有信息的WeatherTimely对象
    public Object searchWeatherTimely(String locationId){
        SQLiteDatabase db=dbHelper.getReadableDatabase();

        Cursor cursor=db.query("WeatherTimely",null,"locationId=?",
                new String[]{locationId},null,null,null);

        WeatherTimely weatherTimely=new WeatherTimely();//要返回的对象
        int index;
        if(cursor.moveToFirst()) {
            weatherTimely.setLocationId(locationId);

            index = cursor.getColumnIndex("mid");
            weatherTimely.setMid(cursor.getInt(index));
            index = cursor.getColumnIndex("obsTime");
            weatherTimely.setObsTime(cursor.getString(index));
            index = cursor.getColumnIndex("temperature");
            weatherTimely.setTemp(cursor.getString(index));
            index = cursor.getColumnIndex("feelsLike");
            weatherTimely.setFeelsLike(cursor.getString(index));
            index = cursor.getColumnIndex("icon");
            weatherTimely.setIcon(cursor.getInt(index));
            index = cursor.getColumnIndex("text");
            weatherTimely.setText(cursor.getString(index));
            index = cursor.getColumnIndex("wind360");
            weatherTimely.setWind360(cursor.getString(index));
            index = cursor.getColumnIndex("windDir");
            weatherTimely.setWindDir(cursor.getString(index));
            index = cursor.getColumnIndex("windScale");
            weatherTimely.setWindScale(cursor.getString(index));
            index = cursor.getColumnIndex("windSpeed");
            weatherTimely.setWindSpeed(cursor.getString(index));
            index = cursor.getColumnIndex("humidity");
            weatherTimely.setHumidity(cursor.getString(index));
            index = cursor.getColumnIndex("precip");
            weatherTimely.setPrecip(cursor.getString(index));
            index = cursor.getColumnIndex("pressure");
            weatherTimely.setPressure(cursor.getString(index));
            index = cursor.getColumnIndex("vis");
            weatherTimely.setVis(cursor.getString(index));
            index = cursor.getColumnIndex("cloud");
            weatherTimely.setCloud(cursor.getString(index));
            index = cursor.getColumnIndex("dew");
            weatherTimely.setDew(cursor.getString(index));
            cursor.close();
            db.close();
           // Log.d("TAG", weatherTimely.toString());
            return weatherTimely;
        }else{
            Log.d("TAG", "null");db.close();
            return null;//返回空，即表中无要查询的数据
        }

         //


    }

//查询指定城市的近24小时天气，返回装载有信息的List<WeatherHourly>
    public List<WeatherHourly> searchWeatherHourly(String locationId){
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        Cursor cursor=db.query("WeatherHourly",null,"locationId=?",
                new String[]{locationId},null,null,null);

        List<WeatherHourly> list=new ArrayList<>();
        int index;
        if(cursor.moveToFirst()){
            do{
                WeatherHourly weatherHourly=new WeatherHourly();
                weatherHourly.setLocationId(locationId);

                index=cursor.getColumnIndex("mid");
                weatherHourly.setMid(cursor.getInt(index));
                index=cursor.getColumnIndex("fxTime");
                weatherHourly.setFxTime(cursor.getString(index));
                index=cursor.getColumnIndex("temperature");
                weatherHourly.setTemp(cursor.getString(index));
                index=cursor.getColumnIndex("icon");
                weatherHourly.setIcon(cursor.getInt(index));
                index=cursor.getColumnIndex("text");
                weatherHourly.setText(cursor.getString(index));
                index=cursor.getColumnIndex("wind360");
                weatherHourly.setWind360(cursor.getString(index));
                index=cursor.getColumnIndex("windDir");
                weatherHourly.setWindDir(cursor.getString(index));
                index=cursor.getColumnIndex("windScale");
                weatherHourly.setWindScale(cursor.getString(index));
                index=cursor.getColumnIndex("windSpeed");
                weatherHourly.setWindSpeed(cursor.getString(index));
                index=cursor.getColumnIndex("humidity");
                weatherHourly.setHumidity(cursor.getString(index));
                index=cursor.getColumnIndex("precip");
                weatherHourly.setPrecip(cursor.getString(index));
                index=cursor.getColumnIndex("pressure");
                weatherHourly.setPressure(cursor.getString(index));
                index=cursor.getColumnIndex("pop");
                weatherHourly.setPop(cursor.getString(index));
                index=cursor.getColumnIndex("cloud");
                weatherHourly.setCloud(cursor.getString(index));
                index=cursor.getColumnIndex("dew");
                weatherHourly.setDew(cursor.getString(index));

                list.add(weatherHourly);
            }while(cursor.moveToNext());db.close();
            cursor.close();
        return list;
        }else{
            db.close();
            cursor.close();
           return null;//返回空，即表中没有要查询的数据
        }
    }

//查询指定城市的近7天天气，返回装载有信息的List<WeatherDaily>
    public List<WeatherDaily> searchWeatherDaily(String locationId){
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        Cursor cursor=db.query("WeatherDaily",null,"locationId=?",
                new String[]{locationId},null,null,null);
        List<WeatherDaily> list=new ArrayList<>();
        int index;
        if(cursor.moveToFirst()){
            do{
                WeatherDaily weatherDaily=new WeatherDaily();

               weatherDaily.setLocationId(locationId);

                index=cursor.getColumnIndex("mid");
                weatherDaily.setMid(cursor.getInt(index));
                index=cursor.getColumnIndex("fxDate");
                weatherDaily.setFxDate(cursor.getString(index));
                index=cursor.getColumnIndex("sunrise");
                weatherDaily.setSunrise(cursor.getString(index));
                index=cursor.getColumnIndex("sunset");
                weatherDaily.setSunset(cursor.getString(index));
                index=cursor.getColumnIndex("moonrise");
                weatherDaily.setMoonrise(cursor.getString(index));
                index=cursor.getColumnIndex("moonset");
                weatherDaily.setMoonset(cursor.getString(index));
                index=cursor.getColumnIndex("moonPhase");
                weatherDaily.setMoonPhase(cursor.getString(index));
                index=cursor.getColumnIndex("moonPhaseIcon");
                weatherDaily.setMoonPhaseIcon(cursor.getInt(index));
                index=cursor.getColumnIndex("tempMax");
                weatherDaily.setTempMax(cursor.getString(index));
                index=cursor.getColumnIndex("tempMin");
                weatherDaily.setTempMin(cursor.getString(index));
                index=cursor.getColumnIndex("iconDay");
                weatherDaily.setIconDay(cursor.getInt(index));
                index=cursor.getColumnIndex("textDay");
                weatherDaily.setTextDay(cursor.getString(index));
                index=cursor.getColumnIndex("iconNight");
                weatherDaily.setIconNight(cursor.getInt(index));
                index=cursor.getColumnIndex("textNight");
                weatherDaily.setTextNight(cursor.getString(index));

                index=cursor.getColumnIndex("wind360Day");
                weatherDaily.setWind360Day(cursor.getString(index));
                index=cursor.getColumnIndex("windDirDay");
                weatherDaily.setWindDirDay(cursor.getString(index));
                index=cursor.getColumnIndex("windScaleDay");
                weatherDaily.setWindScaleDay(cursor.getString(index));
                index=cursor.getColumnIndex("windSpeedDay");
                weatherDaily.setWindSpeedDay(cursor.getString(index));
                index=cursor.getColumnIndex("humidity");
                weatherDaily.setHumidity(cursor.getString(index));
                index=cursor.getColumnIndex("precip");
                weatherDaily.setPrecip(cursor.getString(index));
                index=cursor.getColumnIndex("pressure");
                weatherDaily.setPressure(cursor.getString(index));
                index=cursor.getColumnIndex("vis");
                weatherDaily.setVis(cursor.getString(index));
                index=cursor.getColumnIndex("cloud");
                weatherDaily.setCloud(cursor.getString(index));
                index=cursor.getColumnIndex("uvIndex");
                weatherDaily.setUvIndex(cursor.getString(index));

                list.add(weatherDaily);

            }while(cursor.moveToNext());
         //   list.get(1);db.close();
            cursor.close();
           return list;
        }else{cursor.close();db.close();
            return null;//返回空，即数据库中无要查询的数据

           // return searchWeatherDaily(locationId);
        }
    }

    //下面是对各类数据的删除功能，一般是表中的所有数据通通删除
    //一般城市信息不删除哦
    public void deleteAll() {
        SQLiteDatabase db=dbHelper.getWritableDatabase();
       // db.delete("City",null,null);
        db.delete("LifeIndices",null,null);

        db.delete("AirIndices",null,null);

       db.delete("WeatherTimely",null,null);

        db.delete("WeatherHourly",null,null);

        db.delete("WeatherDaily",null,null);db.close();
       // Toast.makeText(mContext,"delete succeed~",Toast.LENGTH_SHORT)db.close();
            //    .show();
    }

    //下面是删除表中所有24h天气信息的方法
    public void deleteAll24hData(){
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        db.delete("WeatherHourly",null,null);db.close();
        //Toast.makeText(mContext,"delete succeed~",Toast.LENGTH_SHORT)
            //    .show();
    }

    /**
     * 删除所有关于指定locationId的信息
     * @param locationId
     * 指定的locationId
     */
    public void deleteLocationIdInfo(String locationId){
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        db.delete("WeatherTimely","locationId=?",new String[]{locationId});
        db.delete("WeatherHourly","locationId=?",new String[]{locationId});
        db.delete("WeatherDaily","locationId=?",new String[]{locationId});
        db.delete("LifeIndices","locationId=?",new String[]{locationId});
        db.delete("AirIndices","locationId=?",new String[]{locationId});
        db.close();
    }

    //删除时间表的函数，调试用，实际根本用不到
    public void deleteTimeRecord(){
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        db.delete("TimeRecord",null,null);db.close();
      //  Toast.makeText(mContext,"delete succeed~",Toast.LENGTH_SHORT)
             //   .show();
    }

    public void searchNewTime(){
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        db.close();
    }

}

